// Protocol Buffers for GWT
// Copyright 2010 Thomas Broyer  All rights reserved.
// http://code.google.com/p/protobuf-gwt/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package net.ltgt.gwt.protobuf.compiler.client;

import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor.Type;
import com.google.protobuf.compiler.JavaHelpers;

import java.io.IOException;
import java.io.Writer;

/**
 * @author t.broyer@ltgt.net Thomas Broyer
 * Based on the initial work of:
 * @author kenton@google.com Kenton Varda
 * Based on original Protocol Buffers design by
 * Sanjay Ghemawat, Jeff Dean, and others.
 */
public class MessageFieldGenerator extends SingularFieldGenerator {

  protected MessageFieldGenerator(FieldDescriptor descriptor) {
    super(descriptor, JavaHelpers.getClassName(descriptor.getMessageType()));
  }

  @Override
  public void generateBuilderMembers(Writer output) throws IOException {
    output.write("public boolean has" + capitalizedName + "() { return result.has" + capitalizedName + "(); }\n");
    output.write("public " + typeName + " get" + capitalizedName + "() { return result.get" + capitalizedName + "(); }\n");
    output.write("public Builder set" + capitalizedName + "(" + typeName + " value) {\n");
    output.write("  if (value == null) { throw new NullPointerException(); }\n");
    output.write("  result." + name + "_ = value;\n");
    output.write("  return this;\n");
    output.write("}\n");
    output.write("public Builder set" + capitalizedName + "(" + typeName + ".Builder builderForValue) {\n");
    output.write("  if (builderForValue == null) { throw new NullPointerException(); }\n");
    output.write("  result." + name + "_ = builderForValue.build();\n");
    output.write("  return this;\n");
    output.write("}\n");
    output.write("public Builder merge" + capitalizedName + "(" + typeName + " value) {\n");
    output.write("  if (result." + name + "_ != null &&\n");
    output.write("      !" + typeName + ".isDefaultInstance(result." + name + "_)) {\n");
    output.write("    result." + name + "_ = " + typeName + ".newBuilder(result." + name + "_).mergeFrom(value).buildPartial();\n");
    output.write("  } else {\n");
    output.write("    result." + name + "_ = value;\n");
    output.write("  }\n");
    output.write("  return this;\n");
    output.write("}\n");
    output.write("public Builder clear" + capitalizedName + "() {\n");
    output.write("  result." + name + "_ = null;\n");
    output.write("  return this;\n");
    output.write("}\n");
  }

  @Override
  public void generateMembers(Writer output) throws IOException {
    output.write("private " + typeName + " " + name + "_;\n");
    output.write("public boolean has" + capitalizedName + "() { return " + name + "_ != null; }\n");
    output.write("public " + typeName + " get" + capitalizedName + "() {\n");
    output.write("  return has" + capitalizedName + "() ? " + name + "_ : " + typeName + ".getDefaultInstance();\n");
    output.write("}\n");
  }

  @Override
  public void generateMergingCode(Writer output) throws IOException {
    output.write("if (other.has" + capitalizedName + "()) {\n");
    output.write("  merge" + capitalizedName + "(other." + name + "_);\n");
    output.write("}\n");
  }

  @Override
  public void generateParsingCode(Writer output) throws IOException {
    output.write("case " + descriptor.getNumber() + ": {\n");
    output.write("  " + typeName + ".Builder subBuilder = " + typeName + ".newBuilder();\n");
    output.write("  if (has" + capitalizedName + "()) {\n");
    output.write("    subBuilder.mergeFrom(get" + capitalizedName + "());\n");
    output.write("  }\n");
    // TODO: recursion limit
    output.write("  subBuilder.mergeFrom(streamReader);\n");
    output.write("  set" + capitalizedName + "(subBuilder.buildPartial());\n");
    output.write("  break;\n");
    output.write("}\n");
  }

  @Override
  public void generateSerializationCode(Writer output) throws IOException {
    output.write("if (has" + capitalizedName + "()) {\n");
    output.write("  streamWriter.writeInt(" + descriptor.getNumber() + ");\n");
    output.write("  " + typeName + "_CustomFieldSerializer.serialize(streamWriter, get" + capitalizedName + "());\n");
    output.write("}\n");
  }

  @Override
  public void generateDescriptorCode(Writer output) throws IOException {
    output.write("new com.google.protobuf.Descriptors.MessageFieldDescriptor(this,\n");
    output.write("    \"" + descriptor.getName() + "\", " + descriptor.getNumber() + ", " + descriptor.getIndex() + ", " + descriptor.isRequired() + ") {\n");
    output.write("  public com.google.protobuf.Descriptors.Descriptor getMessageType() {\n");
    output.write("    return " + boxedTypeName + ".getDescriptor();\n");
    output.write("  }\n");
    output.write("  public " + boxedTypeName + ".Builder newBuilderForField() {\n");
    output.write("    return " + boxedTypeName + ".newBuilder();\n");
    output.write("  }\n");
    if (descriptor.getType() == Type.GROUP) {
      output.write("  public com.google.protobuf.Descriptors.FieldDescriptor.Type getType() {\n");
      output.write("    return com.google.protobuf.Descriptors.FieldDescriptor.Type.GROUP;\n");
      output.write("  }");
    }
    output.write("},\n");
  }
}
